package csu.web.mypetstore.web.servlet;

import csu.web.mypetstore.domain.Account;
import csu.web.mypetstore.domain.Order;
import csu.web.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class ListOrderServlet  extends HttpServlet {
    private static  final  String LIST_ORDER = "/WEB-INF/jsp/order/listOrder.jsp";
    private List<Order> orderList;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        OrderService orderService = new OrderService();
        Account account = (Account) session.getAttribute("loginAccount");
        if(account != null) {
            orderList = orderService.getOrdersByUsername(account.getUsername());
            session.setAttribute("orderList", orderList);
            req.getRequestDispatcher(LIST_ORDER).forward(req, resp);
        }else{
            resp.sendRedirect("signonForm");
        }
    }
}
